<?php

namespace app\agent\controller;

use app\models\{Business, PlatClass, Recpeople, PlatSons, Goods, GoodsSpec, Region, BusinessArea, Compent, User as Users, Order as Orders, OrderGoods, Payment, Coupon};
use think\Db;
use think\Validate;

class Order extends Base
{
    public function orderdetail()
    {
        $ordersn = $this->request->get('ordersn/d', 0);
        $list = (new OrderGoods())->where(['ordersn' => $ordersn])->paginate(10, false, ['query' => $this->request->param()]);
        foreach ($list as $lists) {
            $lists['images'] = json_decode($lists['images'], true);
        }
        return view(null, [
            'list' => $list
        ]);
    }

    public function order2()
    {
        $results = (new Orders())->order(['times' => 'desc', 'created' => 'desc']);
        $data = $this->request->get();
        $results = $results->where(['cityid|areaid|streetid' => $this->request->agent['id']]);
        if (!empty($data['ordersn'])) {
            $results = $results->where('ordersn', $data['ordersn']);
        }
        $province = (new Region())->where(['level' => 1])->select();
        $city = [];
        $area = [];
        $street = [];
        if (!empty($data['province'])) {
            $city_top = (new Region())->where(['level' => 1, 'area_code' => $data['province']])->value('id') ?? 0;
            $city = (new Region())->where(['level' => 2, 'parent_id' => $city_top])->select();
            $results = $results->where(['province' => $data['province']]);
        }
        if (!empty($data['city'])) {
            $area_top = (new Region())->where(['level' => 2, 'area_code' => $data['city']])->value('id') ?? 0;
            $area = (new Region())->where(['level' => 3, 'parent_id' => $area_top])->select();
            $results = $results->where(['city' => $data['city']]);
        }
        if (!empty($data['area'])) {
            $street_top = (new Region())->where(['level' => 3, 'area_code' => $data['area']])->value('id') ?? 0;
            $street = (new Region())->where(['level' => 4, 'parent_id' => $street_top])->select();
            $results = $results->where(['area' => $data['area']]);
        }
        if (!empty($data['street'])) {
            $results = $results->where(['street' => $data['street']]);
        }
        $results = $results->where('status', 2)->where(['tuikuan' => [0, 3, 4]])->paginate(10, false, ['query' => $this->request->param()]);
        foreach ($results as $result) {
            $result['people'] = (new Recpeople())->where(['id' => $result['putorder']])->find() ?? [];
            $result['business'] = (new Business())->where(['id' => $result['businessid']])->value('title');
            $result['status_name'] = $result->status_text;
            $result['province_name'] = (new Region())->where(['area_code' => $result['province'], 'level' => 1])->value('name');
            $result['city_name'] = (new Region())->where(['area_code' => $result['city'], 'level' => 2])->value('name');
            $result['area_name'] = (new Region())->where(['area_code' => $result['area'], 'level' => 3])->value('name');
            $result['street_name'] = (new Region())->where(['id' => $result['street'], 'level' => 4])->value('name');
            $result['user'] = (new Users())->field('nickName,avatarUrl')->where('id', $result['user_id'])->find();
            $goods = (new OrderGoods())->where(['ordersn' => $result['ordersn']])->select();
            foreach ($goods as $good) {
                $good['images'] = json_decode($good['images'], true);
            }
            $result['good'] = $goods;
            //预估佣金计算
            $result['income'] = 0;
            $incomeparms = json_decode($result['incomeparms'], true);
            if ($result['cityid'] ==  $this->request->agent['id']) {
                $result['income'] = $incomeparms['city_money'] ?? 0;
            }
            if ($result['areaid'] ==  $this->request->agent['id']) {
                $result['income'] = $incomeparms['area_money'] ?? 0;
            }
            if ($result['streetid'] ==  $this->request->agent['id']) {
                $result['income'] = $incomeparms['street_money'] ?? 0;
            }
        }
        return view(null, [
            'list' => $results,
            'province' => $province,
            'city' => $city,
            'area' => $area,
            'street' => $street,
        ]);
    }

    public function order3()
    {
        $results = (new Orders())->order(['times' => 'desc', 'created' => 'desc']);
        $data = $this->request->get();
        $results = $results->where(['cityid|areaid|streetid' => $this->request->agent['id']]);
        if (!empty($data['ordersn'])) {
            $results = $results->where('ordersn', $data['ordersn']);
        }
        $province = (new Region())->where(['level' => 1])->select();
        $city = [];
        $area = [];
        $street = [];
        if (!empty($data['province'])) {
            $city_top = (new Region())->where(['level' => 1, 'area_code' => $data['province']])->value('id') ?? 0;
            $city = (new Region())->where(['level' => 2, 'parent_id' => $city_top])->select();
            $results = $results->where(['province' => $data['province']]);
        }
        if (!empty($data['city'])) {
            $area_top = (new Region())->where(['level' => 2, 'area_code' => $data['city']])->value('id') ?? 0;
            $area = (new Region())->where(['level' => 3, 'parent_id' => $area_top])->select();
            $results = $results->where(['city' => $data['city']]);
        }
        if (!empty($data['area'])) {
            $street_top = (new Region())->where(['level' => 3, 'area_code' => $data['area']])->value('id') ?? 0;
            $street = (new Region())->where(['level' => 4, 'parent_id' => $street_top])->select();
            $results = $results->where(['area' => $data['area']]);
        }
        if (!empty($data['street'])) {
            $results = $results->where(['street' => $data['street']]);
        }
        $results = $results->where('status', 3)->where(['tuikuan' => [0, 3, 4]])->paginate(10, false, ['query' => $this->request->param()]);
        foreach ($results as $result) {
            $result['people'] = (new Recpeople())->where(['id' => $result['putorder']])->find() ?? [];
            $result['business'] = (new Business())->where(['id' => $result['businessid']])->value('title');
            $result['status_name'] = $result->status_text;
            $result['province_name'] = (new Region())->where(['area_code' => $result['province'], 'level' => 1])->value('name');
            $result['city_name'] = (new Region())->where(['area_code' => $result['city'], 'level' => 2])->value('name');
            $result['area_name'] = (new Region())->where(['area_code' => $result['area'], 'level' => 3])->value('name');
            $result['street_name'] = (new Region())->where(['id' => $result['street'], 'level' => 4])->value('name');
            $result['user'] = (new Users())->field('nickName,avatarUrl')->where('id', $result['user_id'])->find();
            $goods = (new OrderGoods())->where(['ordersn' => $result['ordersn']])->select();
            foreach ($goods as $good) {
                $good['images'] = json_decode($good['images'], true);
            }
            $result['good'] = $goods;
            //预估佣金计算
            $result['income'] = 0;
            $incomeparms = json_decode($result['incomeparms'], true);
            if ($result['cityid'] ==  $this->request->agent['id']) {
                $result['income'] = $incomeparms['city_money'] ?? 0;
            }
            if ($result['areaid'] ==  $this->request->agent['id']) {
                $result['income'] = $incomeparms['area_money'] ?? 0;
            }
            if ($result['streetid'] ==  $this->request->agent['id']) {
                $result['income'] = $incomeparms['street_money'] ?? 0;
            }
        }
        return view(null, [
            'list' => $results,
            'province' => $province,
            'city' => $city,
            'area' => $area,
            'street' => $street,
        ]);
    }

    public function order4()
    {
        $results = (new Orders())->order(['times' => 'desc', 'created' => 'desc']);
        $data = $this->request->get();
        $results = $results->where(['cityid|areaid|streetid' => $this->request->agent['id']]);
        if (!empty($data['ordersn'])) {
            $results = $results->where('ordersn', $data['ordersn']);
        }
        $province = (new Region())->where(['level' => 1])->select();
        $city = [];
        $area = [];
        $street = [];
        if (!empty($data['province'])) {
            $city_top = (new Region())->where(['level' => 1, 'area_code' => $data['province']])->value('id') ?? 0;
            $city = (new Region())->where(['level' => 2, 'parent_id' => $city_top])->select();
            $results = $results->where(['province' => $data['province']]);
        }
        if (!empty($data['city'])) {
            $area_top = (new Region())->where(['level' => 2, 'area_code' => $data['city']])->value('id') ?? 0;
            $area = (new Region())->where(['level' => 3, 'parent_id' => $area_top])->select();
            $results = $results->where(['city' => $data['city']]);
        }
        if (!empty($data['area'])) {
            $street_top = (new Region())->where(['level' => 3, 'area_code' => $data['area']])->value('id') ?? 0;
            $street = (new Region())->where(['level' => 4, 'parent_id' => $street_top])->select();
            $results = $results->where(['area' => $data['area']]);
        }
        if (!empty($data['street'])) {
            $results = $results->where(['street' => $data['street']]);
        }
        $results = $results->where('status', 4)->paginate(10, false, ['query' => $this->request->param()]);
        foreach ($results as $result) {
            $result['people'] = (new Recpeople())->where(['id' => $result['putorder']])->find() ?? [];
            $result['business'] = (new Business())->where(['id' => $result['businessid']])->value('title');
            $result['status_name'] = $result->status_text;
            $result['province_name'] = (new Region())->where(['area_code' => $result['province'], 'level' => 1])->value('name');
            $result['city_name'] = (new Region())->where(['area_code' => $result['city'], 'level' => 2])->value('name');
            $result['area_name'] = (new Region())->where(['area_code' => $result['area'], 'level' => 3])->value('name');
            $result['street_name'] = (new Region())->where(['id' => $result['street'], 'level' => 4])->value('name');
            $result['user'] = (new Users())->field('nickName,avatarUrl')->where('id', $result['user_id'])->find();
            $goods = (new OrderGoods())->where(['ordersn' => $result['ordersn']])->select();
            foreach ($goods as $good) {
                $good['images'] = json_decode($good['images'], true);
            }
            $result['good'] = $goods;
            //预估佣金计算
            $result['income'] = 0;
            $incomeparms = json_decode($result['incomeparms'], true);
            if ($result['cityid'] ==  $this->request->agent['id']) {
                $result['income'] = $incomeparms['city_money'] ?? 0;
            }
            if ($result['areaid'] ==  $this->request->agent['id']) {
                $result['income'] = $incomeparms['area_money'] ?? 0;
            }
            if ($result['streetid'] ==  $this->request->agent['id']) {
                $result['income'] = $incomeparms['street_money'] ?? 0;
            }
        }
        return view(null, [
            'list' => $results,
            'province' => $province,
            'city' => $city,
            'area' => $area,
            'street' => $street,
        ]);
    }
}